Repository Public
1// Vrai-Prix — analyse multimodale d'une annonce : démarrage (POST, sur demande seulement) et suivi/lecture (GET).2import { NextRequest, NextResponse } from "next/server";3import { AnalysisError, getById, getLatest, startAnalysis } from "@/lib/cost/ai/service";4import { clientKey, rateLimit } from "@/lib/cost/cache";56export const runtime = "nodejs";7export const maxDuration = 60;89export async function POST(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {10 const { uid } = await params;11 if (!rateLimit(`ai:${clientKey(req)}`, 3, 10 * 60_000)) return NextResponse.json({ error: "Trop d'analyses lancées — réessayez dans quelques minutes.", code: "rate_limited" }, { status: 429 });12 try {13 const r = await startAnalysis(decodeURIComponent(uid), { force: false });14 return NextResponse.json(r, { status: r.reused ? 200 : 202 });15 } catch (e) {16 return errorResponse(e);17 }18}1920export async function GET(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {21 const { uid } = await params;22 const id = req.nextUrl.searchParams.get("id");23 try {24 const v = id ? getById(id) : getLatest(decodeURIComponent(uid));25 if (!v) return NextResponse.json({ status: "none" }, { headers: { "Cache-Control": "no-store" } });26 if (v.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "analyse d'une autre annonce" }, { status: 400 });27 // en cours : réponse légère pour le polling28 if (v.status !== "completed" && v.status !== "failed") return NextResponse.json({ id: v.id, status: v.status, stage: v.stage, version: v.version, imageCount: v.imageCount, createdAt: v.createdAt }, { headers: { "Cache-Control": "no-store" } });29 return NextResponse.json(v, { headers: { "Cache-Control": "no-store" } });30 } catch (e) {31 return errorResponse(e);32 }33}3435function errorResponse(e: unknown) {36 if (e instanceof AnalysisError) return NextResponse.json({ error: e.message, code: e.code }, { status: e.status });37 return NextResponse.json({ error: (e as Error).message ?? "erreur", code: "error" }, { status: 500 });38}39